var key = 'token'

function xhr(obj) {
	// var token = JSON.parse(sessionStorage.getItem('token'))
	var baseUrl = 'http://2916yw1620.zicp.vip'
	// var baseUrl='http://192.168.134.1:5555'
	var xhr = new XMLHttpRequest()
	xhr.open(obj.methods || 'get', baseUrl + obj.url)
	if (obj.header) {
		for (var key in obj.header) {
			xhr.setRequestHeader(key, obj.header[key])
		}
	} else {
		xhr.setRequestHeader('Content-Type', 'application/json')
		xhr.setRequestHeader('token', getToken(key))
	}
	xhr.send(obj.data || null)
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4 && xhr.status == 200) {
			if (xhr.response) {
				// console.log(xhr.response)
				var objResponse = JSON.parse(xhr.response)
				// if (objResponse.code == 200) {
					// console.log(objResponse)
					obj.success && obj.success(objResponse.data)
				// } else {
					// popupMsg('error', objResponse.msg || '服务异常')
				// }
			}

		}
	}
}
function getToken() {
	return sessionStorage.getItem(key)
}

function setToken(token) {
	return sessionStorage.setItem(key, token)
}

function removeToken() {
	return sessionStorage.removeItem(key)
}

// 图片上传

// fileSC.addEventListener('change', function () {
// 	var file = fileSC.files[0]
// 	var form = new FormData()
// 	form.append('file', file)
// 	xhr({
// 		url: '/upload/upload',
// 		methods: 'post',
// 		header: {},
// 		data: form,
// 		success: function (res) {
// 			console.log(res)
// 			formUrl = res.data
// 		}
// 	})
// })
function NoneB(i) {
	i.style.display = 'none'
}

function BlockN(i) {
	i.style.display = 'block'
}
function FlexB(i){
	i.style.display = 'flex'
}
//从身份证号中获取生日
function getBirth(idCard) {
	var birthday = "";
  if(idCard != null && idCard != ""){
	  if(idCard.length == 15){
		  birthday = "19"+idCard.slice(6,12);
	  } else if(idCard.length == 18){
		  birthday = idCard.slice(6,14);
	  }	
	  birthday = birthday.replace(/(.{4})(.{2})/,"$1-$2-");
	  //通过正则表达式来指定输出格式为:1990-01-01
  }	
  return birthday;
}
// 判断单选框是否选中
function validateRadio() {
	const radios = document.getElementsByName("xb");
	let isChecked = false;

	for (let i = 0; i < radios.length; i++) {
		if (radios[i].checked) {
			isChecked = true;
			break;
		}
	}
	return isChecked
}